using System.Security.Cryptography;

namespace GrpcDemo.Authentication;

public class RsaKeyHelper
{
    public RSA PrivateKey { get; set; }
    public RSA PublicKey { get; set; }
    
    // ReSharper disable once ConvertToPrimaryConstructor
    public RsaKeyHelper(string privateKeyPath, string publicKeyPath)
    {
        PrivateKey = LoadPrivateKey(privateKeyPath);
        PublicKey = LoadPublicKey(publicKeyPath);
    }
    
    private static RSA LoadPrivateKey(string privateKeyPath)
    {
        var privateKey = File.ReadAllBytes(privateKeyPath);
        var rsa = RSA.Create();
        rsa.ImportRSAPrivateKey(privateKey, out _);
        return rsa;
    }

    private static RSA LoadPublicKey(string publicKeyPath)
    {
        var publicKey = File.ReadAllBytes(publicKeyPath);
        var rsa = RSA.Create();
        rsa.ImportRSAPublicKey(publicKey, out _);
        return rsa;
    }
}